package com.ysyc.driver_ee.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.TextView;

import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.api.HttpResource;
import com.ysyc.driver_ee.api.NetworkCallback;
import com.ysyc.driver_ee.api.RetrofitClient;
import com.ysyc.driver_ee.base.BaseFragment;
import com.ysyc.driver_ee.model.StatistiaVoListModel;
import com.ysyc.driver_ee.model.StatisticalVo;
import com.ysyc.driver_ee.widget.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * <p>描述：(个人统计)</p>
 * 作者： Chan<br>
 * 日期： 2018/10/31 10:47<br>
 * 版本： v1.0<br>
 */
public class StatisticsPersonalFragment extends BaseFragment {
    @BindView(R.id.tl_statistics_personal_view)
    TabLayout tlStatisticsPersonalView;
    @BindView(R.id.cvp_statistics_view)
    CustomViewPager cvpStatisticsView;
    @BindView(R.id.tv_week_total_water_view)
    TextView tvWeekTotalWaterView;
    @BindView(R.id.tv_today_order_num_view)
    TextView tvTodayOrderNumView;
    @BindView(R.id.tv_total_order_num_view)
    TextView tvTotalOrderNumView;
    @BindView(R.id.tv_today_water_view)
    TextView tvTodayWaterView;
    @BindView(R.id.tv_total_water_view)
    TextView tvTotalWaterView;


    private final String[] TAB_TITLES = new String[]{"近期流水", "近期成单"};
    //Tab 数目
    private final int COUNT = TAB_TITLES.length;
    //Fragment 数组
    private final BaseFragment[] TAB_FRAGMENTS = new BaseFragment[COUNT];
    public static final String TYPE_KEY = "type";
    public List<StatistiaVoListModel> statistiaVoListModels;
    private int todayNum;
    private int todayWater;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_statistics_personal;
    }

    @Override
    protected void onFragmentCreate(View view) {
        super.onFragmentCreate(view);

        initNetworkCallback();
    }


    private void initNetworkCallback() {
        NetworkCallback<StatisticalVo> statisticalVoNetworkCallback = new NetworkCallback<>(new HttpResource<StatisticalVo>() {
            @Override
            public void onSuccess(StatisticalVo statisticalVo) {
                statistiaVoListModels = statisticalVo.statistiaVoListModels;
                initTabLayout();
                initView(statisticalVo);

            }

            @Override
            public void onFailure(String errorDes) {

            }

            @Override
            public void onLoading(Dialog dialog) {

            }
        });

        Observable<StatisticalVo> statisticalVoObservable = RetrofitClient.getInstance().getHttpServices().obtainStatisticalVo();
        statisticalVoNetworkCallback.postRequest(statisticalVoObservable, null);
    }

    private void initView(StatisticalVo statisticalVo) {
        List<StatistiaVoListModel> statistiaVoListModels = statisticalVo.statistiaVoListModels;
        int totalAmount = 0;

        for (int i = 0; i < statistiaVoListModels.size(); i++) {
            totalAmount = totalAmount + statistiaVoListModels.get(i).water;
            if (i==0){
                todayNum = statistiaVoListModels.get(i).num;
                todayWater = statistiaVoListModels.get(i).water;
            }
        }
        tvWeekTotalWaterView.setText(totalAmount+"");
        tvTotalOrderNumView.setText(statisticalVo.toMonthNum+"");
        tvTotalWaterView.setText(statisticalVo.toMonth+"");
        tvTodayOrderNumView.setText(todayNum+"");
        tvTodayWaterView.setText(todayWater+"");

    }

//    private void initView(List<StatistiaVoListModel> statistiaVoListModels) {
//        int totalAmount = 0;
//        for (StatistiaVoListModel statistiaVoListModel : statistiaVoListModels) {
//            totalAmount =totalAmount+ statistiaVoListModel.num;
//        }
//        tvWeekTotalWaterView.setText(totalAmount);
//        tvTotalOrderNumView.setText();
//
//    }

    private void initTabLayout() {
        for (int i = 0; i < TAB_TITLES.length; i++) {
            Bundle bundle = new Bundle();
            bundle.putInt(TYPE_KEY, i);
            bundle.putParcelableArrayList("statistiaVoListModels", (ArrayList<? extends Parcelable>) statistiaVoListModels);
            BarChartFragment fragment = BarChartFragment.newInstance(BarChartFragment.class, bundle);
            TAB_FRAGMENTS[i] = fragment;
            tlStatisticsPersonalView.addTab(tlStatisticsPersonalView.newTab().setText(TAB_TITLES[i]));
        }
        cvpStatisticsView.setOffscreenPageLimit(3);
        StatisticsPersonalFragment.MyViewPagerAdapter mAdapter = new StatisticsPersonalFragment.MyViewPagerAdapter(getChildFragmentManager());
        cvpStatisticsView.setAdapter(mAdapter);
        cvpStatisticsView.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tlStatisticsPersonalView));
        tlStatisticsPersonalView.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(cvpStatisticsView));

    }

    /**
     * @description: ViewPager 适配器
     */
    private class MyViewPagerAdapter extends FragmentPagerAdapter {

        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return TAB_FRAGMENTS[position];
        }

        @Override
        public int getCount() {
            return TAB_TITLES.length;
        }

    }


}
